package com.jh.paymentchannel.util.xjy;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

public class  QuickPaySDKConfig {

    private Logger LOG = LoggerFactory.getLogger(getClass());
    public static final String FILE_NAME = "quickpay_sdk.properties";

    private String t1RequestUrl;// quickpay.sdk.t1.request.url
    private String t0RequestUrl;
    private String t1RequestOpenCardSDKUrl;
    private String t1RequestConsumeSMSSDKUrl;
    private String t1RequestConsumeSDKUrl;
    private String t1RequestSDKUrl;
    private String t0RequestSDKUrl;
    private String version;
    private String utf8;
    private String t1AppId;
    private String t1AppCode;
    private String t1PubicKey;
    private String t1SDKAppId;
    private String t1SDKAppCode;
    private String t1SDKPubicKey;
    private String t1ChannelType;
    private String t0AppId;
    private String t0AppCode;
    private String t0PubicKey;
    private String t0SDKAppId;
    private String t0SDKAppCode;
    private String t0SDKPubicKey;
    private String t0ChannelType;
    private String t1FrontUrl;
    private String t1BackUrl;
    private String t0FrontUrl;
    private String t0BackUrl;
    private String t1RequestParams;
    private String t1SDKOpenCardRequestParams;
    private String t1SDKConsumeSMSRequestParams;
    private String t1SDKConsumeRequestParams;
    private String t0RequestParams;
    
    private String d0RequestOpenCardSDKUrl;
    private String d0RequestConsumeSMSSDKUrl;
    private String d0RequestConsumeSDKUrl;
    private String d0SDKOpenCardRequestParams;
    private String d0SDKConsumeSMSRequestParams;
    private String d0SDKConsumeRequestParams;
    

    private static final String T1_REQUEST_URL = "quickpay.sdk.t1.request.url";
    private static final String T0_REQUEST_URL = "quickpay.sdk.t0.request.url";
    private static final String T1_REQUEST_SDK_URL = "quickpay.sdk.t1.request.sdk.url";
    private static final String T0_REQUEST_SDK_URL = "quickpay.sdk.t0.request.sdk.url";
    private static final String VERSION = "quickpay.sdk.version";
    private static final String UTF8_ENCODING = "quickpay.sdk.utf8";
    private static final String T1_APP_ID = "quickpay.sdk.t1.appid";
    private static final String T1_APP_CODE = "quickpay.sdk.t1.appcode";
    private static final String T1_PUBLIC_KEY = "quickpay.sdk.t1.pubkey";
    private static final String T1_SDK_APP_ID = "quickpay.sdk.t1.sdk.appid";
    private static final String T1_SDK_APP_CODE = "quickpay.sdk.t1.sdk.appcode";
    private static final String T1_SDK_PUBLIC_KEY = "quickpay.sdk.t1.sdk.pubkey";
    private static final String T1_CHANNEL_TYPE = "quickpay.sdk.t1.channeltype";
    private static final String T0_APP_ID = "quickpay.sdk.t0.appid";
    private static final String T0_APP_CODE = "quickpay.sdk.t0.appcode";
    private static final String T0_PUBLIC_KEY = "quickpay.sdk.t0.pubkey";
    private static final String T0_SDK_APP_ID = "quickpay.sdk.t0.sdk.appid";
    private static final String T0_SDK_APP_CODE = "quickpay.sdk.t0.sdk.appcode";
    private static final String T0_SDK_PUBLIC_KEY = "quickpay.sdk.t0.sdk.pubkey";
    private static final String T0_CHANNEL_TYPE = "quickpay.sdk.t0.channeltype";
    private static final String T1_FRONT_URL = "quickpay.sdk.t1.fronturl";
    private static final String T1_BACK_URL = "quickpay.sdk.t1.backurl";
    private static final String T0_FRONT_URL = "quickpay.sdk.t0.fronturl";
    private static final String T0_BACK_URL = "quickpay.sdk.t0.backurl";
    private static final String T1_REQUEST_PARAMS = "quickpay.sdk.t1.request.params";
    private static final String T0_REQUEST_PARAMS = "quickpay.sdk.t0.request.params";
    private static final String T1_REQUEST_OPENCARD_SDK_PARAMS = "quickpay.sdk.wtz.t1.opencard.request.params";
    private static final String T1_REQUEST_CONSUMESMS_SDK_PARAMS = "quickpay.sdk.wtz.t1.consumesms.request.params";
    private static final String T1_REQUEST_CONSUME_SDK_PARAMS = "quickpay.sdk.wtz.t1.consume.request.params";
    private static final String T1_REQUEST_OPENCARD_SDK_URL = "quickpay.sdk.t1.request.sdk.opencard.url";
    private static final String T1_REQUEST_CONSUMESMS_SDK_URL = "quickpay.sdk.t1.request.sdk.consumesms.url";
    private static final String T1_REQUEST_CONSUME_SDK_URL = "quickpay.sdk.t1.request.sdk.consume.url";
    
    private static final String D0_REQUEST_OPENCARD_SDK_PARAMS = "quickpay.sdk.wtz.d0.opencard.request.params";
    private static final String D0_REQUEST_CONSUMESMS_SDK_PARAMS = "quickpay.sdk.wtz.d0.consumesms.request.params";
    private static final String D0_REQUEST_CONSUME_SDK_PARAMS = "quickpay.sdk.wtz.d0.consume.request.params";
    private static final String D0_REQUEST_OPENCARD_SDK_URL = "quickpay.sdk.d0.request.sdk.opencard.url";
    private static final String D0_REQUEST_CONSUMESMS_SDK_URL = "quickpay.sdk.d0.request.sdk.consumesms.url";
    private static final String D0_REQUEST_CONSUME_SDK_URL = "quickpay.sdk.d0.request.sdk.consume.url";
    

    /** 操作对象. */
    private static QuickPaySDKConfig config = new QuickPaySDKConfig();
    /** 属性文件对象. */
    private Properties properties;

    public static QuickPaySDKConfig getConfig() {
        return config;
    }

    public void loadPropertiesFromPath(String rootPath) {
        if (StringUtils.isNotBlank(rootPath)) {
            LOG.info("从路径读取配置文件: " + rootPath + File.separator + FILE_NAME);
            File file = new File(rootPath + File.separator + FILE_NAME);
            InputStream in = null;
            if (file.exists()) {
                try {
                    in = new FileInputStream(file);
                    properties = new Properties();
                    properties.load(in);
                    loadProperties(properties);
                } catch (FileNotFoundException e) {
                    LOG.error(e.getMessage(), e);
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                } finally {
                    if (null != in) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            LOG.error(e.getMessage(), e);
                        }
                    }
                }
            } else {
                // 由于此时可能还没有完成LOG的加载，因此采用标准输出来打印日志信息
                LOG.error(rootPath + FILE_NAME + "不存在,加载参数失败");
            }
        } else {
            loadPropertiesFromSrc();
        }
    }

    public void loadPropertiesFromSrc() {
        InputStream in = null;
        try {
            in = QuickPaySDKConfig.class.getClassLoader().getResourceAsStream(FILE_NAME);
            if (null != in) {
                properties = new Properties();
                try {
                    properties.load(in);
                } catch (IOException e) {
                    throw e;
                }
            } else {
                LOG.error(FILE_NAME + "属性文件未能在classpath指定的目录下 " + QuickPaySDKConfig.class.getClassLoader().getResource("").getPath()
                        + " 找到!");
                return;
            }
            loadProperties(properties);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    LOG.error(e.getMessage(), e);
                }
            }
        }
    }

    public void loadProperties(Properties pro) {
        String value = null;
        value = pro.getProperty(T1_REQUEST_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1RequestUrl = value.trim();
        }
        value = pro.getProperty(T0_REQUEST_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t0RequestUrl = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1RequestSDKUrl = value.trim();
        }
        value = pro.getProperty(T0_REQUEST_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t0RequestSDKUrl = value.trim();
        }
        value = pro.getProperty(VERSION);
        if (StringUtils.isNotBlank(value)) {
            this.version = value.trim();
        }
        value = pro.getProperty(UTF8_ENCODING);
        if (StringUtils.isNotBlank(value)) {
            this.utf8 = value.trim();
        }
        value = pro.getProperty(T1_APP_ID);
        if (StringUtils.isNotBlank(value)) {
            this.t1AppId = value.trim();
        }
        value = pro.getProperty(T1_APP_CODE);
        if (StringUtils.isNotBlank(value)) {
            this.t1AppCode = value.trim();
        }
        value = pro.getProperty(T1_PUBLIC_KEY);
        if (StringUtils.isNotBlank(value)) {
            this.t1PubicKey = value.trim();
        }
        value = pro.getProperty(T1_SDK_APP_ID);
        if (StringUtils.isNotBlank(value)) {
            this.t1SDKAppId = value.trim();
        }
        value = pro.getProperty(T1_SDK_APP_CODE);
        if (StringUtils.isNotBlank(value)) {
            this.t1SDKAppCode = value.trim();
        }
        value = pro.getProperty(T1_SDK_PUBLIC_KEY);
        if (StringUtils.isNotBlank(value)) {
            this.t1SDKPubicKey = value.trim();
        }
        value = pro.getProperty(T1_CHANNEL_TYPE);
        if (StringUtils.isNotBlank(value)) {
            this.t1ChannelType = value.trim();
        }
        value = pro.getProperty(T0_APP_ID);
        if (StringUtils.isNotBlank(value)) {
            this.t0AppId = value.trim();
        }
        value = pro.getProperty(T0_APP_CODE);
        if (StringUtils.isNotBlank(value)) {
            this.t0AppCode = value.trim();
        }
        value = pro.getProperty(T0_PUBLIC_KEY);
        if (StringUtils.isNotBlank(value)) {
            this.t0PubicKey = value.trim();
        }
        value = pro.getProperty(T0_SDK_APP_ID);
        if (StringUtils.isNotBlank(value)) {
            this.t0SDKAppId = value.trim();
        }
        value = pro.getProperty(T0_SDK_APP_CODE);
        if (StringUtils.isNotBlank(value)) {
            this.t0SDKAppCode = value.trim();
        }
        value = pro.getProperty(T0_SDK_PUBLIC_KEY);
        if (StringUtils.isNotBlank(value)) {
            this.t0SDKPubicKey = value.trim();
        }
        value = pro.getProperty(T0_CHANNEL_TYPE);
        if (StringUtils.isNotBlank(value)) {
            this.t0ChannelType = value.trim();
        }
        value = pro.getProperty(T1_FRONT_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1FrontUrl = value.trim();
        }
        value = pro.getProperty(T1_BACK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1BackUrl = value.trim();
        }
        value = pro.getProperty(T0_FRONT_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t0FrontUrl = value.trim();
        }
        value = pro.getProperty(T0_BACK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t0BackUrl = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.t1RequestParams = value.trim();
        }
        value = pro.getProperty(T0_REQUEST_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.t0RequestParams = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_OPENCARD_SDK_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.t1SDKOpenCardRequestParams = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_CONSUMESMS_SDK_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.t1SDKConsumeSMSRequestParams = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_CONSUME_SDK_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.t1SDKConsumeRequestParams = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_OPENCARD_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1RequestOpenCardSDKUrl = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_CONSUMESMS_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1RequestConsumeSMSSDKUrl = value.trim();
        }
        value = pro.getProperty(T1_REQUEST_CONSUME_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.t1RequestConsumeSDKUrl = value.trim();
        }
        
        value = pro.getProperty(D0_REQUEST_OPENCARD_SDK_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.d0SDKOpenCardRequestParams = value.trim();
        }
        value = pro.getProperty(D0_REQUEST_CONSUMESMS_SDK_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.d0SDKConsumeSMSRequestParams = value.trim();
        }
        value = pro.getProperty(D0_REQUEST_CONSUME_SDK_PARAMS);
        if (StringUtils.isNotBlank(value)) {
            this.d0SDKConsumeRequestParams = value.trim();
        }
        value = pro.getProperty(D0_REQUEST_OPENCARD_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.d0RequestOpenCardSDKUrl = value.trim();
        }
        value = pro.getProperty(D0_REQUEST_CONSUMESMS_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.d0RequestConsumeSMSSDKUrl = value.trim();
        }
        value = pro.getProperty(D0_REQUEST_CONSUME_SDK_URL);
        if (StringUtils.isNotBlank(value)) {
            this.d0RequestConsumeSDKUrl = value.trim();
        }
    }

    public String getT1RequestUrl() {
        return t1RequestUrl;
    }

    public void setT1RequestUrl(String t1RequestUrl) {
        this.t1RequestUrl = t1RequestUrl;
    }

    public String getT0RequestUrl() {
        return t0RequestUrl;
    }

    public void setT0RequestUrl(String t0RequestUrl) {
        this.t0RequestUrl = t0RequestUrl;
    }

    public String getT1RequestSDKUrl() {
        return t1RequestSDKUrl;
    }

    public void setT1RequestSDKUrl(String t1RequestSDKUrl) {
        this.t1RequestSDKUrl = t1RequestSDKUrl;
    }

    public String getT0RequestSDKUrl() {
        return t0RequestSDKUrl;
    }

    public void setT0RequestSDKUrl(String t0RequestSDKUrl) {
        this.t0RequestSDKUrl = t0RequestSDKUrl;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getUtf8() {
        return utf8;
    }

    public void setUtf8(String utf8) {
        this.utf8 = utf8;
    }

    public String getT1AppId() {
        return t1AppId;
    }

    public void setT1AppId(String t1AppId) {
        this.t1AppId = t1AppId;
    }

    public String getT1AppCode() {
        return t1AppCode;
    }

    public void setT1AppCode(String t1AppCode) {
        this.t1AppCode = t1AppCode;
    }

    public String getT1PubicKey() {
        return t1PubicKey;
    }

    public void setT1PubicKey(String t1PubicKey) {
        this.t1PubicKey = t1PubicKey;
    }

    public String getT1SDKAppId() {
        return t1SDKAppId;
    }

    public void setT1SDKAppId(String t1sdkAppId) {
        t1SDKAppId = t1sdkAppId;
    }

    public String getT1SDKAppCode() {
        return t1SDKAppCode;
    }

    public void setT1SDKAppCode(String t1sdkAppCode) {
        t1SDKAppCode = t1sdkAppCode;
    }

    public String getT1SDKPubicKey() {
        return t1SDKPubicKey;
    }

    public void setT1SDKPubicKey(String t1sdkPubicKey) {
        t1SDKPubicKey = t1sdkPubicKey;
    }

    public String getT1ChannelType() {
        return t1ChannelType;
    }

    public void setT1ChannelType(String t1ChannelType) {
        this.t1ChannelType = t1ChannelType;
    }

    public String getT0AppId() {
        return t0AppId;
    }

    public void setT0AppId(String t0AppId) {
        this.t0AppId = t0AppId;
    }

    public String getT0AppCode() {
        return t0AppCode;
    }

    public void setT0AppCode(String t0AppCode) {
        this.t0AppCode = t0AppCode;
    }

    public String getT0PubicKey() {
        return t0PubicKey;
    }

    public void setT0PubicKey(String t0PubicKey) {
        this.t0PubicKey = t0PubicKey;
    }

    public String getT0SDKAppId() {
        return t0SDKAppId;
    }

    public void setT0SDKAppId(String t0sdkAppId) {
        t0SDKAppId = t0sdkAppId;
    }

    public String getT0SDKAppCode() {
        return t0SDKAppCode;
    }

    public void setT0SDKAppCode(String t0sdkAppCode) {
        t0SDKAppCode = t0sdkAppCode;
    }

    public String getT0SDKPubicKey() {
        return t0SDKPubicKey;
    }

    public void setT0SDKPubicKey(String t0sdkPubicKey) {
        t0SDKPubicKey = t0sdkPubicKey;
    }

    public String getT0ChannelType() {
        return t0ChannelType;
    }

    public void setT0ChannelType(String t0ChannelType) {
        this.t0ChannelType = t0ChannelType;
    }

    public String getT1FrontUrl() {
        return t1FrontUrl;
    }

    public void setT1FrontUrl(String t1FrontUrl) {
        this.t1FrontUrl = t1FrontUrl;
    }

    public String getT1BackUrl() {
        return t1BackUrl;
    }

    public void setT1BackUrl(String t1BackUrl) {
        this.t1BackUrl = t1BackUrl;
    }

    public String getT0FrontUrl() {
        return t0FrontUrl;
    }

    public void setT0FrontUrl(String t0FrontUrl) {
        this.t0FrontUrl = t0FrontUrl;
    }

    public String getT0BackUrl() {
        return t0BackUrl;
    }

    public void setT0BackUrl(String t0BackUrl) {
        this.t0BackUrl = t0BackUrl;
    }

    public String getT1RequestParams() {
        return t1RequestParams;
    }

    public void setT1RequestParams(String t1RequestParams) {
        this.t1RequestParams = t1RequestParams;
    }

    public String getT0RequestParams() {
        return t0RequestParams;
    }

    public void setT0RequestParams(String t0RequestParams) {
        this.t0RequestParams = t0RequestParams;
    }
    
    public String getT1RequestOpenCardSDKUrl() {
        return t1RequestOpenCardSDKUrl;
    }

    public void setT1RequestOpenCardSDKUrl(String t1RequestOpenCardSDKUrl) {
        this.t1RequestOpenCardSDKUrl = t1RequestOpenCardSDKUrl;
    }
    
    public String getT1SDKOpenCardRequestParams() {
        return t1SDKOpenCardRequestParams;
    }

    public void setT1SDKOpenCardRequestParams(String t1sdkOpenCardRequestParams) {
        t1SDKOpenCardRequestParams = t1sdkOpenCardRequestParams;
    }

    public String getT1SDKConsumeSMSRequestParams() {
        return t1SDKConsumeSMSRequestParams;
    }

    public void setT1SDKConsumeSMSRequestParams(String t1sdkConsumeSMSRequestParams) {
        t1SDKConsumeSMSRequestParams = t1sdkConsumeSMSRequestParams;
    }

    public String getT1SDKConsumeRequestParams() {
        return t1SDKConsumeRequestParams;
    }

    public void setT1SDKConsumeRequestParams(String t1sdkConsumeRequestParams) {
        t1SDKConsumeRequestParams = t1sdkConsumeRequestParams;
    }
    
    public String getT1RequestConsumeSMSSDKUrl() {
        return t1RequestConsumeSMSSDKUrl;
    }

    public void setT1RequestConsumeSMSSDKUrl(String t1RequestConsumeSMSSDKUrl) {
        this.t1RequestConsumeSMSSDKUrl = t1RequestConsumeSMSSDKUrl;
    }

    public String getT1RequestConsumeSDKUrl() {
        return t1RequestConsumeSDKUrl;
    }

    public void setT1RequestConsumeSDKUrl(String t1RequestConsumeSDKUrl) {
        this.t1RequestConsumeSDKUrl = t1RequestConsumeSDKUrl;
    }
    
    public String getD0RequestOpenCardSDKUrl() {
        return d0RequestOpenCardSDKUrl;
    }

    public void setD0RequestOpenCardSDKUrl(String d0RequestOpenCardSDKUrl) {
        this.d0RequestOpenCardSDKUrl = d0RequestOpenCardSDKUrl;
    }

    public String getD0RequestConsumeSMSSDKUrl() {
        return d0RequestConsumeSMSSDKUrl;
    }

    public void setD0RequestConsumeSMSSDKUrl(String d0RequestConsumeSMSSDKUrl) {
        this.d0RequestConsumeSMSSDKUrl = d0RequestConsumeSMSSDKUrl;
    }

    public String getD0RequestConsumeSDKUrl() {
        return d0RequestConsumeSDKUrl;
    }

    public void setD0RequestConsumeSDKUrl(String d0RequestConsumeSDKUrl) {
        this.d0RequestConsumeSDKUrl = d0RequestConsumeSDKUrl;
    }

    public String getD0SDKOpenCardRequestParams() {
        return d0SDKOpenCardRequestParams;
    }

    public void setD0SDKOpenCardRequestParams(String d0sdkOpenCardRequestParams) {
        d0SDKOpenCardRequestParams = d0sdkOpenCardRequestParams;
    }

    public String getD0SDKConsumeSMSRequestParams() {
        return d0SDKConsumeSMSRequestParams;
    }

    public void setD0SDKConsumeSMSRequestParams(String d0sdkConsumeSMSRequestParams) {
        d0SDKConsumeSMSRequestParams = d0sdkConsumeSMSRequestParams;
    }

    public String getD0SDKConsumeRequestParams() {
        return d0SDKConsumeRequestParams;
    }

    public void setD0SDKConsumeRequestParams(String d0sdkConsumeRequestParams) {
        d0SDKConsumeRequestParams = d0sdkConsumeRequestParams;
    }
}
